package com.example.web;

import com.example.domain.BookDatabaseQueryService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
public class BookController {

    private final BookDatabaseQueryService service;
    /** 分页尺寸上限 */
    private static final Integer MAX_PAGE_SIZE = 100;

    public BookController(BookDatabaseQueryService service) {
        this.service = service;
    }

    /**
     * 书目分页查询
     */
    @GetMapping("/list")
    public Response list(@RequestParam(name = "page", defaultValue = "1")Integer pageNum,
                         @RequestParam(name = "size", defaultValue = "5") Integer pageSize) {
        // pageIndex数组下标从0开始, pageNum页码从第1页开始
        int pageIndex = pageNum - 1;
        if (pageNum <= 0) {
            pageIndex = 0;
        }
        // 限制单页最大条数
        if (pageSize > MAX_PAGE_SIZE) {
            pageSize = MAX_PAGE_SIZE;
        }
        // 执行分页查询
        BookDatabaseQueryService.BookQueryCmdResult result = service.queryBookList(pageIndex, pageSize);
        // 构建并返回书目查询结果
        return new BookSearchResponse(200, "OK",
                result.getBookList(),
                result.getNumOfTotalBooks(),
                result.getNumOfBooksInCurrentPage(),
                result.getNumOfPages()
        );
    }

    /**
     * 搜索特定条件匹配的书籍
     *
     * @param request 搜索条件包括ID/ISBN/书名：
     *                其中ISBN编码包含13位数字和可选的横线或空格，格式举例 { "isbnCode": "978-7040429091" }
     *
     * @return 对应该ISBN码的所有记录
     */
    @PostMapping("/search")
    public Response search(@RequestBody BookSearchRequest request,
                           @RequestParam(name = "page", defaultValue = "1")Integer pageNum,
                           @RequestParam(name = "size", defaultValue = "5") Integer pageSize) {
        // 字段检查
        // pageIndex数组下标从0开始, pageNum页码从第1页开始
        int pageIndex = pageNum - 1;
        if (pageNum <= 0) {
            pageIndex = 0;
        }
        // 限制单页最大条数
        if (pageSize > MAX_PAGE_SIZE) {
            pageSize = MAX_PAGE_SIZE;
        }
        if (request.getIsbnCode() == null
                && request.getBookTitle() == null
        ) {
            return new BookSearchResponse(501,
                    // TODO: 允许组合其他字段进行查询
                    "Not Implemented: 当前版本仅支持按isbnCode或bookTitle字段进行查询, 其他字段未实现"
            );
        }
        BookDatabaseQueryService.BookQueryCmdParam param = new BookDatabaseQueryService.BookQueryCmdParam(pageIndex, pageSize,
                "",
                request.getIsbnCode(),
                request.getBookTitle(),
                "",
                "",
                ""
        );
        // 查询并返回结果
        BookDatabaseQueryService.BookQueryCmdResult result = service.queryByIsbnOrBookTitleContaining(param);
        if (result.getErrCode() != 0) {
            return new BookSearchResponse(404, "Not found");
        }
        return new BookSearchResponse(200, "OK",
                result.getBookList(),
                result.getNumOfTotalBooks(),
                result.getNumOfBooksInCurrentPage(),
                result.getNumOfPages()
        );
    }

    /**
     * 创建一本新书
     *
     * @param request 创建新书时所需参数，如下:
     *                - isbnCode: "978-7040429091"
     *                - bookTitle: "三国演义"
     *                - author: "罗贯中"
     *                - publisher: "高等教育出版社"
     *                - briefIntroduction: "《三国演义》又名《三国志演义》、《三国志通俗演义》，是我国小说史上杰出的长篇章回体历史小说。"
     *
     * @return 数据库该条记录的书号ID
     */
    @PostMapping("/create")
    public Response createNewBook(@RequestBody BookCreationRequest request) {
        BookDatabaseQueryService.BookCreationCmdParam cmdParam = new BookDatabaseQueryService.BookCreationCmdParam(
                request.getIsbnCode(),
                request.getBookTitle(),
                request.getAuthor(),
                request.getPublisher(),
                request.getBriefIntroduction()
        );
        BookDatabaseQueryService.BookCreationCmdResult cmdResult = service.createNewBook(cmdParam);
        return new BookCreationResponse(cmdResult.getBookId());
    }
}
